package com.contact.util.util.search;

import java.util.Collection;
import java.util.HashSet;

import com.contact.util.util.filters.DataFilter;
import com.trg.search.Search;

public class SearchInfo {

    private Class<?> searchClass;
    private PaginationInfo paginationInfo;
    private Collection<DataFilter> filters;

    public SearchInfo() {
        this(null);
    }

    public SearchInfo(Class<?> searchClass) {
        this(searchClass, PaginationInfo.createPaginationInfo());
    }

    public SearchInfo(Class<?> searchClass, PaginationInfo paginationInfo) {
        this(searchClass, paginationInfo, null);
    }

    public SearchInfo(Class<?> searchClass, PaginationInfo paginationInfo, Collection<DataFilter> filters) {
        super();
        setSearchClass(searchClass);
        setPaginationInfo(paginationInfo);
        setFilters(filters);
    }

    public final Class<?> getSearchClass() {
        return searchClass;
    }

    public final void setSearchClass(Class<?> searchClass) {
        this.searchClass = searchClass;
    }

    public final IPaginationInfo getPaginationInfo() {
        return paginationInfo;
    }

    public final void setPaginationInfo(PaginationInfo paginationInfo) {
        this.paginationInfo = paginationInfo;
    }

    public final Collection<DataFilter> getFilters() {
        return filters;
    }

    public final void setFilters(Collection<DataFilter> filters) {
        this.filters = filters;
    }

    //************ Helper functions

    public final void addFilter(DataFilter filter) {
        if (filters == null) {
            setFilters(new HashSet<DataFilter>());
        }

        filters.add(filter);
    }

    public final void addFilters(DataFilter... filters) {
        if (filters == null) {
            return;
        }

        for (DataFilter filter : filters) {
            addFilter(filter);
        }
    }

    public final Search getSearch() {

        //TODO not sure the search class works if the POJO is not a PersistableObject

        return SearchHelper.createSearch(getFilters(), getPaginationInfo(), getSearchClass());

    }

    //************ Pass through functions ************

    public final String getSortColumn() {
        return paginationInfo.getResultItemSort().getColumn();
    }

    public final void setSortColumn(String sortColumn) {
        paginationInfo.getResultItemSort().setColumn(sortColumn);
    }

    public final boolean getAscending() {
        return paginationInfo.getResultItemSort().getAscending();
    }

    public final void setAscending(boolean sortAsc) {
        paginationInfo.getResultItemSort().setAscending(sortAsc);
    }

    public final int getFirstRow() {
        return paginationInfo.getFirstRow();
    }

    public final void setFirstRow(int firstRow) {
        paginationInfo.setFirstRow(firstRow);
    }

    public final int getMaxResults() {
        return paginationInfo.getMaxResults();
    }

    public final void setMaxResults(int maxResults) {
        paginationInfo.setMaxResults(maxResults);
    }

}
